package com.cskaoyan.user.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cskaoyan.mall.commons.result.ResponseData;
import com.cskaoyan.mall.commons.result.ResponseUtil;
import com.cskaoyan.mall.commons.util.CookieUtil;
import com.cskaoyan.user.constants.UserRetCode;
import com.cskaoyan.user.dto.*;
import com.cskaoyan.user.service.IKaptchaService;
import com.cskaoyan.user.service.ILoginOutService;
import com.cskaoyan.user.service.ILoginService;
import com.cskaoyan.user.service.IRegisterService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * @author Tezel
 * @since 2022/07/29 15:18
 */
@Slf4j
@RestController
@RequestMapping("user")
public class UserController {

    @Autowired
    ILoginService loginService;

    @Autowired
    ILoginOutService loginOutService;

    @Autowired
    IKaptchaService iKaptchaService;

    @Autowired
    IRegisterService registerService;


    /**
     * 用户注册
     * @author Tezel
     * @date 2022/07/30
     */
    @PostMapping("register")
    public ResponseData register(@RequestBody Map<String,String> map ,HttpServletRequest request){
        String userName = map.get("userName");
        String userPwd = map.get("userPwd");
        String email = map.get("email");
        String captcha = map.get("captcha");

        //验证验证码
        KaptchaCodeResponse kaptchaCodeResponse = checkCaptcha(request,captcha);
        if(!UserRetCode.SUCCESS.getCode().equals(kaptchaCodeResponse.getCode())){
            return new ResponseUtil().setErrorMsg(kaptchaCodeResponse.getMsg());
        }

        //用户表插入数据
        UserRegisterRequest userRegisterRequest = new UserRegisterRequest();
        userRegisterRequest.setUserName(userName);
        userRegisterRequest.setEmail(email);
        userRegisterRequest.setUserPwd(userPwd);

        UserRegisterResponse registerResponse = registerService.register(userRegisterRequest);

        if(registerResponse.getCode().equals(UserRetCode.SUCCESS.getCode())){
            return new ResponseUtil().setData(null);
        }

        return new ResponseUtil().setErrorMsg(registerResponse.getMsg());
    }

    /**
     * 用户注册激活
     * @author Tezel
     * @date 2022/07/31
     */
    @RequestMapping("verify")
    public ResponseData registerVerify(String uid,String username){
        UserVerifyRequest userVerifyRequest = new UserVerifyRequest();
        userVerifyRequest.setUuid(uid);
        userVerifyRequest.setUserName(username);

        UserVerifyResponse userVerifyResponse = registerService.verify(userVerifyRequest);

        if(UserRetCode.SUCCESS.getCode().equals(userVerifyResponse.getCode())){
            return new ResponseUtil().setData(null);
        }

        return new ResponseUtil().setErrorMsg(userVerifyResponse.getMsg());
    }



    /**
     * 用户登录
     * @author Tezel
     * @date 2022/07/29
     */
    @PostMapping("login")
    public ResponseData login(@RequestBody Map<String,String> map , HttpServletRequest request, HttpServletResponse httpResponse){
        String userName = map.get("userName");
        String userPwd = map.get("userPwd");
        String captcha = map.get("captcha");


        //验证验证码
        KaptchaCodeResponse kaptchaCodeResponse = checkCaptcha(request,captcha);
        if(!UserRetCode.SUCCESS.getCode().equals(kaptchaCodeResponse.getCode())){
            return new ResponseUtil().setErrorMsg(kaptchaCodeResponse.getMsg());
        }


        //用户登录
        UserLoginRequest userLoginRequest = new UserLoginRequest();
        userLoginRequest.setUserName(userName);
        userLoginRequest.setPassword(userPwd);
        UserLoginResponse userLoginResponse = loginService.login(userLoginRequest,httpResponse);

        //成功
        if(UserRetCode.SUCCESS.getCode().equals(userLoginResponse.getCode())){
            return new ResponseUtil().setData(userLoginResponse,"success");
        }

        //失败
        return new ResponseUtil().setErrorMsg(userLoginResponse.getMsg());
    }


    /**
     * 验证码验证
     * @author Tezel
     * @date 2022/07/30
     */
    private KaptchaCodeResponse checkCaptcha(HttpServletRequest request, String captcha) {

        KaptchaCodeRequest kaptchaCodeRequest = new KaptchaCodeRequest();

        String uuid = CookieUtil.getCookieValue(request, "kaptcha_uuid");
        kaptchaCodeRequest.setUuid(uuid);
        kaptchaCodeRequest.setCode(captcha);

        KaptchaCodeResponse response = iKaptchaService.validateKaptchaCode(kaptchaCodeRequest);

        return response;
    }


    /**
     * 登录验证
     * @author Tezel
     * @date 2022/07/30
     */

    @GetMapping("login")
    public ResponseData loginVerify(HttpServletRequest servletRequest){


        //获取请求参数中的token
        String userInfo = (String) servletRequest.getHeader("user_info");
        JSONObject object = JSON.parseObject(userInfo);
        Long uid = Long.parseLong(object.get("uid").toString());


        //获取到token,通过uid查询用户信息返回
        QueryMemberRequest queryMemberRequest = new QueryMemberRequest();

        queryMemberRequest.setUserId(uid);

        LoginVerifyResponse loginVerifyResponse = loginService.queryMember(queryMemberRequest);

        return new ResponseUtil<>().setData(loginVerifyResponse,"success");
    }

    /**
     * 用户退出
     * @author Tezel
     * @date 2022/07/30
     */
    @GetMapping("loginOut")
    public ResponseData loginOut(HttpServletRequest servletRequest,HttpServletResponse servletResponse) {

        loginOutService.loginOut(servletRequest,servletResponse);

        return new ResponseUtil<>().setData(null);
    }

}
